/*
 * $Id: ItemByCategoryInfo.java 27 2013-05-20 13:31:39Z antonyhands@gmail.com $
 */
package ru.olympus.antonyhands.ao.ui.info;

import java.util.List;

import ru.olympus.antonyhands.ao.UIHelper;
import ru.olympus.antonyhands.ao.data.AOData;
import ru.olympus.antonyhands.ao.data.entities.AOItem;
import ru.olympus.antonyhands.ao.data.entities.ItemCategory;
import ru.olympus.antonyhands.ao.data.util.ByTitleComparator;
import ru.olympus.antonyhands.ao.data.util.EItemParentType;
import ru.olympus.antonyhands.ao.data.util.ItemByUniversalParentFilter;
import ru.olympus.antonyhands.ao.ui.AddEditAOItem;
import ru.olympus.antonyhands.ao.util.GoogleIterable;
import ru.olympus.antonyhands.ao.util.StringUtilities;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.VerticalLayout;

/**
 * @author AntonyHands
 * @since 16.05.2013
 */
public class ItemByCategoryInfo extends CustomComponent
{
    private AOItem _item;

    @AutoGenerated
    private VerticalLayout mainLayout;

    @AutoGenerated
    private ComboBox comboBoxItem;

    @AutoGenerated
    private ComboBox comboBoxCategory;

    private AOItemInfo itemInfo;

    private Button editButton = new Button("Редактировать");

    private static final long serialVersionUID = 5599396016628593706L;

    public ItemByCategoryInfo()
    {
        this(null);
    }

    /**
     * The constructor should first build the main layout, set the
     * composition root and then do any custom initialization.
     *
     * The constructor will not be automatically regenerated by the
     * visual editor.
     */
    public ItemByCategoryInfo(AOItem item)
    {
        _item = item;
        buildMainLayout();
        setCompositionRoot(mainLayout);
        for (ItemCategory category : AOData.getInstance().listItemCategories())
        {
            comboBoxCategory.addItem(category.getCode());
            comboBoxCategory.setItemCaption(category.getCode(), category.getTitle());
        }
        // предустановленный предмет
        if (_item != null)
        {
            comboBoxCategory.setValue(_item.getItemCategory().getCode());
            comboBoxItem.setValue(_item.getCode());
        }
    }

    @AutoGenerated
    private VerticalLayout buildMainLayout()
    {
        // common part: create layout
        mainLayout = new VerticalLayout();

        // comboBoxCategory
        comboBoxCategory = new ComboBox();
        comboBoxCategory.setCaption("Категория");
        comboBoxCategory.setTextInputAllowed(false);
        comboBoxCategory.setImmediate(true);
        comboBoxCategory.addListener(new ValueChangeListener()
        {
            private static final long serialVersionUID = 6421977753089240484L;

            @Override
            public void valueChange(ValueChangeEvent event)
            {
                Property prop = event.getProperty();
                refreshItemsList((String)prop.getValue());
            }
        });
        mainLayout.addComponent(comboBoxCategory);

        // comboBoxItem
        comboBoxItem = new ComboBox();
        comboBoxItem.setCaption("Предмет");
        comboBoxItem.setTextInputAllowed(false);
        comboBoxItem.setNullSelectionAllowed(false);
        comboBoxItem.setImmediate(true);
        comboBoxItem.addListener(new ValueChangeListener()
        {
            private static final long serialVersionUID = 8524192094312523057L;

            @Override
            public void valueChange(ValueChangeEvent event)
            {
                String prop = (String)event.getProperty().getValue();
                if (!StringUtilities.isEmpty(prop))
                {
                    itemInfo.setAOItem(AOData.getInstance().getItemByCode(prop));
                }
            }
        });
        mainLayout.addComponent(comboBoxItem);

        itemInfo = new AOItemInfo();
        mainLayout.addComponent(itemInfo);

        mainLayout.addComponent(editButton);
        editButton.addListener(new ClickListener()
        {
            private static final long serialVersionUID = -2181860544500491055L;

            @Override
            public void buttonClick(ClickEvent event)
            {
                AOItem item = AOData.getInstance().getItemByCode((String)comboBoxItem.getValue());
                UIHelper.changeCurrentComponent(new AddEditAOItem(item, new ItemByCategoryInfo(item)));
            }
        });
        
        return mainLayout;
    }

    private void refreshItemsList(String categoryCode)
    {
        Predicate<AOItem> predicate = Predicates.alwaysTrue();
        if (!StringUtilities.isEmpty(categoryCode))
        {
            predicate = new ItemByUniversalParentFilter(EItemParentType.ITEM_CATEGORY, categoryCode);
        }
        List<AOItem> list = GoogleIterable
                .with(AOData.getInstance().listItems())
                .filter(predicate)
                .sort(new ByTitleComparator())
                .toArrayList();
        comboBoxItem.removeAllItems();
        for (AOItem item : list)
        {
            comboBoxItem.addItem(item.getCode());
            comboBoxItem.setItemCaption(item.getCode(), item.getTitle());
        }
    }
}
